package com.wunu.zn.api.lock.action;


import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wunu.zn.api.lock.model.SysUserEntity;
import com.wunu.zn.api.lock.model.UserEntity;
import common.exception.BusinessException;
import common.plugin.mybatis.Db;
import common.support.MobileParam;
import common.support.phoneMsg.PhoneMsgService;
import common.utils.FileUtil;
import common.utils.StringUtil;
import properties.ClientMsg;
@Controller
@RequestMapping("lock/user")
public class LockUserAction {
	private static Logger log = Logger.getLogger(LockUserAction.class);
	/**
	 * 注册
	 * 
	 * @param param
	 * @return
	 */
	@RequestMapping("/regist")
	@ResponseBody
	public String regist(MobileParam param) {
		UserEntity user = param.getModel(UserEntity.class, "account, password");
		if (param.getPara("sys") != null) {
			user.set("sys", param.getPara("sys"));
		}
		try {
			UserEntity.registEn(user);
			UserEntity.setDefaultInfo(user);
			user.update();
			param.put("userId", user.get("id"));
		} catch (BusinessException e) {
			log.error("/m/user/regist" + e.getMessage());
			return param.renderFail(e.getCode(), e.getMessage());
		}
		return param.renderSuccess(ClientMsg.USER_REGIST_SUCCESS, "注册成功");

	}
	/**
	 * 登录
	 * @param param
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public String login(MobileParam param) {
		String account  = param.getPara("account");
		String password =  param.getPara("password");
		try {
			UserEntity user = UserEntity.login(account,password);
			if (!user.getStr("password").equals(password)) {
				return param.renderFail(ClientMsg.USER_PASSWORD_ERROR, "密码错误,请输入正确密码");
			}
			param.put("isMerchant", user.get("isMerchant"));
			return param.put("userId", user.getId()).renderSuccess(ClientMsg.USER_LOGIN_SUCCESS, "登录成功");
		} catch (BusinessException e) {
			log.error("/m/user/login" + e.getMessage());
			return param.renderFail(ClientMsg.USER_ACCOUNT_NO_EXIST_NO, "账号不存在");
		}

	}
	
	/**
	 * 重置密码－1：发送验证码
	 * 
	 * @param param
	 * @return
	 */
	@RequestMapping("/sendCaptcha")
	public String sendCaptcha(MobileParam param) {
		PhoneMsgService pm = new PhoneMsgService();
		String account = param.getPara("account");
		String register = param.getPara("register");
		UserEntity user = UserEntity.findByAccount(account);
		if (user != null && "Y".equals(register)) {
			return param.renderFail(ClientMsg.USER_ACCOUNT_EXIST_NO, "账号已经存在");
		} else if (user == null && "N".equals(register)) {
			return param.renderFail(ClientMsg.USER_ACCOUNT_NO_EXIST_NO, "账号不存在");
		}
		String captcha = StringUtil.randNum(6);
//		AppCaptcha.add(account, captcha, AppCaptcha.TYPE_RESET_PASSWORD);
		if (StringUtil.isMobileNumber(account)) {
			// 发短信
			pm.sendResetPassCaptcha(account, captcha, param.getLanguage());
		}
		param.put("validTime", 60);
		return param.put("captcha", captcha).renderSuccess("", "");
	}
	/**
	 * 重置密码－2：设置新密码
	 * 
	 * @param param
	 * @return
	 */
	@RequestMapping("/resetPass")
	@ResponseBody
	
	
	
	public String resetPass(MobileParam param) {
		String account = param.getPara("account");
		String password = param.getPara("password");
		UserEntity.resetPass(account, password);
		return param.renderSuccess(ClientMsg.USER_RESET_PASSWOR_SUCCESS, "新密码设置成功");
	}
	/**
	 * 修改密码
	 * @param param
	 * @return
	 */
	@RequestMapping("/updatePwd")
	@ResponseBody
	public String updatePwd(MobileParam param) {
		String userId = param.getPara("userId");
		String oldPwd = param.getPara("oldPwd");
		String newPwd = param.getPara("newPwd");
		UserEntity user = UserEntity.findUserByIdStr(userId);
		if (!user.getStr("password").equals(oldPwd)) {
			return param.renderFail(ClientMsg.OLD_PASSWORD_NOT_RIGHT, "旧密码不正确");
		}
		user.set("password", newPwd);
		user.update();
		return param.renderSuccess("", "");

	}
	/**
	 * 保存用户信息
	 * 
	 * @param param
	 * @return
	 */
	@RequestMapping("/saveInfo")
	@ResponseBody
	public String saveInfo(MobileParam param) {
		int userId = param.getMoblieUserId();

		UserEntity user = param.getModel(UserEntity.class, "nickName, photo,age, gender, phone ,sign");
		user.set(UserEntity.COLUMN_ID, userId);
		UserEntity.setDefaultInfo(user);
		try {
			user.update();
			return param.renderSuccess(ClientMsg.USER_INFO_SAVE_SUCCESS, "保存成功");
		} catch (Exception e) {
			return param.renderFail(ClientMsg.USER_INFO_SAVE_FAIL, "保存失败");
		}
	}
	/**
	 * 获取用户详细信息
	 * 
	 * @param param
	 * @return
	 */
	@RequestMapping("/getInfo")
	@ResponseBody
	public String getInfo(MobileParam param) {
		String userId = param.getPara("userId");
		UserEntity user = UserEntity.findUserByIdStr(userId);
		if (user != null) {
			user.formatTime("createTime");
			return param.put("user", user.getAttrs()).renderSuccess("", "");
		} else {
			return param.put("user", new UserEntity()).renderSuccess("", "");
		}
	}
	/**
	 * 绑定房源
	 * @param param
	 * @return
	 */
	@RequestMapping("/bind")
	@ResponseBody
	public String bind(MobileParam param) {
/*		Integer nextId = param.getParaToInt("nextId", 0);
		Integer pageSize = param.getParaToInt("pageSize", AppConst.PAGE_SIZE);*/
		String userId = param.getPara("userId");
		String phone = param.getPara("phone");
		SysUserEntity sysuser =	SysUserEntity.findAdminByPhone(phone);
		String operator = sysuser.get("USERNAME");
		UserEntity user = UserEntity.findUserByIdStr(userId);
		user.set("operator", operator);
	    int result = Db.update(user);
	    if(result==1){
			/*List<Map<String, Object>> rooms = RoomEntity.findRoomList(operator, nextId, pageSize);
			if (!rooms.isEmpty() && !rooms.isEmpty()) {
				param.put("nextId", rooms.get(rooms.size() - 1).get("id"));
				param.put("pageSize", pageSize);
				param.put("rooms", rooms);*/
				return param.renderSuccess("1", "OK");
	/*		} else {
				return param.renderFail("0", "Empty");
			}*/
	    }else{
	    	return param.renderFail("0", "NO");
	    }
	}

	/**
	 * 头像上传(照片上传)
	 */
	@RequestMapping("picupload")
	@ResponseBody
	public String picupload(HttpServletRequest request, MobileParam param){
		FileUtil fu = new FileUtil();
		try {
			//上传图片
			String imgurl = fu.upload(request/*, param.getPara("file")*/);
			if(imgurl == null || "1".equals(imgurl)){
				return param.renderFail("", "1");//表示上传失败(没有文件或者路径创建失败)
			}else if( "2".equals(imgurl)){
				return param.renderFail("", "2");//表示上传失败(上传文件格式不对,仅支持.gif/.jpg/.png/.jpeg格式图片)
			}else{
				return param.put("url", imgurl).renderSuccess("", "0");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error( "UserAction -- picupload() : " + e.getMessage());
			return param.renderFail();
		}
	}
	
	
	
}
